<?php

class SearchController extends FrontEndController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view', 'getpeople', 'getgroups', 'getaudios', 'getvideos'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionIndex()
	{
		$tabs = array(
			'groups',
			'audio',
			'video'
		);
		
		$_GET['limit'] = 10;
		
		if(isset($_GET['tab']) && in_array($_GET['tab'], $tabs)){
			$current_tab = $_GET['tab'];
		}else{
			$current_tab = 'people';
		}	
				
		if($current_tab == 'groups'){
			$data = $this->getDataGroups();
		}elseif($current_tab == 'audio'){
			$data = $this->getDataAudios();
		}elseif($current_tab == 'video'){	
			$data = $this->getDataVideos();
		}elseif($current_tab == 'people'){	
			$data = $this->getDataPeople();
		}
	
		$countries = Country::model()->findAll();
		
		$groupTypes = GroupType::model()->findAll();
		
		$loveStatus = LoveStatus::model()->findAll();
		
		$this->render('index', array(
			'current_tab'=>$current_tab,
			'search'=>isset($_REQUEST['text']) ? $_REQUEST['text'] : '',
			'filter_country'=>isset($_GET['filter_country']) ? $_GET['filter_country'] : false,
			'filter_age_from'=>isset($_GET['filter_age_from']) ? $_GET['filter_age_from'] : false,
			'filter_age_to'=>isset($_GET['filter_age_to']) ? $_GET['filter_age_to'] : false,
			'filter_city'=>isset($_GET['filter_city']) ? $_GET['filter_city'] : '',
			'filter_teach'=>isset($_GET['filter_teach']) ? $_GET['filter_teach'] : '',
			'filter_work'=>isset($_GET['filter_work']) ? $_GET['filter_work'] : '',
			'filter_love_status'=>isset($_GET['filter_love_status']) ? $_GET['filter_love_status'] : false,
			'data'=>$data,
			'countries'=>$countries,
			'groupTypes'=>$groupTypes,
			'loveStatus'=>$loveStatus,
			'limit'=>$_GET['limit'],
			'offset'=>$_GET['limit']
		));
	}
	
	private function getDataPeople(){
		$criteria = new CDbCriteria();
		$criteria->order = 'full_name ASC';
			
		if(!empty($_GET['offset'])){
			$criteria->offset = $_GET['offset'];
		}
		
		if(!empty($_GET['limit'])){
			$criteria->limit = $_GET['limit'];
		}else{
			$criteria->limit = 10;
		}
			
		$condition = '';
		
		if(!empty($_GET['offset'])){
			$criteria->offset = $_GET['offset'];
		}
			
		if(!empty($_REQUEST['text'])){
			$match = addcslashes($_REQUEST['text'], '%_');
			$condition = '`full_name` LIKE "%' . $match . '%"';
		}
			
		if(!empty($_GET['filter_country'])){
			if($condition){
				$condition .= ' AND country_id="' . $_GET['filter_country'] . '"';
			}else{
				$condition .= 'country_id="' . $_GET['filter_country'] . '"';
			}
		}
		
		if(!empty($_GET['filter_city'])){
			$match = addcslashes($_GET['filter_city'], '%_');
			if($condition){
				$condition .= ' AND `city` LIKE "%' . $match . '%"';
			}else{
				$condition .= '`city` LIKE "%' . $match . '%"';
			}
		}		
		
		if(!empty($_GET['filter_love_status'])){
			if($condition){
				$condition .= ' AND love_status="' . $_GET['filter_love_status'] . '"';
			}else{
				$condition .= 'love_status="' . $_GET['filter_love_status'] . '"';
			}
		}		
		
		if(!empty($_GET['filter_teach'])){
			$match = addcslashes($_GET['filter_teach'], '%_');
			if($condition){
				$condition .= ' AND `teach` LIKE "%' . $match . '%"';
			}else{
				$condition .= '`teach` LIKE "%' . $match . '%"';
			}
		}			
		
		if(!empty($_GET['filter_work'])){
			$match = addcslashes($_GET['filter_work'], '%_');
			if($condition){
				$condition .= ' AND `work` LIKE "%' . $match . '%"';
			}else{
				$condition .= '`work` LIKE "%' . $match . '%"';
			}
		}		
		
		if(!empty($_GET['filter_age_from'])){
			if($condition){
				$condition .= ' AND birthday <= DATE_SUB(curdate(), INTERVAL ' . $_GET['filter_age_from'] . ' YEAR)';
			}else{
				$condition .= 'birthday <= DATE_SUB(curdate(), INTERVAL ' . $_GET['filter_age_from'] . ' YEAR)';
			}
		}
		
		if(!empty($_GET['filter_age_to'])){
			if($condition){
				$condition .= ' AND birthday >= DATE_SUB(curdate(), INTERVAL ' . $_GET['filter_age_to'] . ' YEAR)';
			}else{
				$condition .= 'birthday >= DATE_SUB(curdate(), INTERVAL ' . $_GET['filter_age_to'] . ' YEAR)';
			}
		}
			
		if(!empty($_GET['filter_sex'])){
			if($condition){
				$condition .= ' AND sex="' . $_GET['filter_sex'] . '"';
			}else{
				$condition .= 'sex="' . $_GET['filter_sex'] . '"';
			}
		}
			
		$criteria->condition=$condition;
			
		return Users::model()->findAll( $criteria );		
	}
	
	private function getDataGroups(){
		$criteria = new CDbCriteria();
		$criteria->order = 'name ASC';

		if(!empty($_GET['offset'])){
			$criteria->offset = $_GET['offset'];
		}
			
		if(!empty($_GET['limit'])){
			$criteria->limit = $_GET['limit'];
		}else{
			$criteria->limit = 10;
		}
			
		$condition = '';
			
		if(!empty($_REQUEST['text'])){
			$match = addcslashes($_REQUEST['text'], '%_');
			$condition = '(name LIKE "%' . $_REQUEST['text'] . '%" OR description LIKE "%' . $_REQUEST['text'] . '%")';
		}
		
		if(!empty($_GET['filter_country'])){
			if($condition){
				$condition .= ' AND country_id="' . $_GET['filter_country'] . '"';
			}else{
				$condition .= 'country_id="' . $_GET['filter_country'] . '"';
			}
		}
			
		if(!empty($_GET['filter_group_type'])){
			if($condition){
				$condition .= ' AND approve="' . $_GET['filter_group_type'] . '"';
			}else{
				$condition .= 'approve="' . $_GET['filter_group_type'] . '"';
			}
		}
		
		$criteria->condition=$condition;
		
		return Groups::model()->findAll( $criteria );		
	}
	
	private function getDataAudios(){
		$criteria = new CDbCriteria();
		$criteria->order = 'artist ASC';
		
		if(!empty($_GET['offset'])){
			$criteria->offset = $_GET['offset'];
		}
		
		if(!empty($_GET['limit'])){
			$criteria->limit = $_GET['limit'];
		}else{
			$criteria->limit = 10;
		}
		
		$condition = '';
		
		if(!empty($_REQUEST['text'])){
			$match = addcslashes($_REQUEST['text'], '%_');
			$condition = '(name LIKE "%' . $_REQUEST['text'] . '%" OR artist LIKE "%' . $_REQUEST['text'] . '%")';
		}
		
		$criteria->condition=$condition;
		
		return Music::model()->findAll( $criteria );	
	}
	
	private function getDataVideos(){	
		$criteria = new CDbCriteria();
		$criteria->order = 'name ASC';
		
		if(!empty($_GET['offset'])){
			$criteria->offset = $_GET['offset'];
		}
		
		if(!empty($_GET['limit'])){
			$criteria->limit = $_GET['limit'];
		}else{
			$criteria->limit = 10;
		}
		
		$condition = '';
		
		if(!empty($_REQUEST['text'])){
			$match = addcslashes($_REQUEST['text'], '%_');
			$condition = 'name LIKE "%' . $_REQUEST['text'] . '%"';
		}
		
		$criteria->condition=$condition;
		
		return VideoUser::model()->findAll( $criteria );		
	}
	
	public function actionGetPeople(){
		if (Yii::app()->request->isAjaxRequest) {
			$offset = 0;
				
			if(!empty($_GET['offset'])){
				$offset = $_GET['offset'];
			}
			
			if(!empty($_GET['limit'])){
				$offset = $offset + $_GET['limit'];
			}			
			
			$this->renderPartial('_peopleList',array(
				'people' => $this->getDataPeople(),
				'limit' => (!empty($_GET['limit'])) ? $_GET['limit'] : '',
				'offset' => $offset					
			));
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}		
	}
	
	public function actionGetGroups(){
		if (Yii::app()->request->isAjaxRequest) {
			$offset = 0;
			
			if(!empty($_GET['offset'])){
				$offset = $_GET['offset'];
			}
				
			if(!empty($_GET['limit'])){
				$offset = $offset + $_GET['limit'];
			}

			$this->renderPartial('_groupList',array(
				'groups' => $this->getDataGroups(),
				'limit' => (!empty($_GET['limit'])) ? $_GET['limit'] : '',
				'offset' => $offset
			));
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}
	}	
	
	public function actionGetAudios(){
		if (Yii::app()->request->isAjaxRequest) {
			$offset = 0;
				
			if(!empty($_GET['offset'])){
				$offset = $_GET['offset'];
			}
			
			if(!empty($_GET['limit'])){
				$offset = $offset + $_GET['limit'];
			}
		
			$this->renderPartial('_audioList',array(
				'audios' => $this->getDataAudios(),
				'limit' => (!empty($_GET['limit'])) ? $_GET['limit'] : '',
				'offset' => $offset
			));
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}		
	}
	
	public function actionGetVideos(){
		if (Yii::app()->request->isAjaxRequest) {
			$offset = 0;
			
			if(!empty($_GET['offset'])){
				$offset = $_GET['offset'];
			}
				
			if(!empty($_GET['limit'])){
				$offset = $offset + $_GET['limit'];
			}
		
			$this->renderPartial('_videoList',array(
				'videos' => $this->getDataVideos(),
				'limit' => (!empty($_GET['limit'])) ? $_GET['limit'] : '',
				'offset' => $offset
			));	
		}else{
			$this->redirect(Yii::app()->createUrl('profile/index', array('id'=>Yii::app()->user->id)));
		}		
	}
}
